#pragma once

#include <iostream>
#include <assert.h>
using namespace std;

namespace xsc
{
	template <typename T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		//vector(){}

		vector() = default;

		vector(const vector<T>& v)
		{
			reserve(v.size());
			for (auto& e : v)
			{
				push_back(e);
			}
		}

		vector(size_t n, const T& val = T())
		{
			reserve(n);
			for (size_t i = 0; i < n; ++i)
			{
				push_back(val);
			}
		}
		
		vector(int n, const T& val = T())
		{
			reserve(n);
			for (int i = 0; i < n; ++i)
			{
				push_back(val);
			}
		}

		template <class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			int len = last - first;
			reserve(len);
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}

		vector<T>& operator=(vector<T> v)
		{
			swap(v);
		}

		~vector()
		{
			delete[]_start;
			_start = nullptr;
			_finish = nullptr;
			_end_of_storage = nullptr;
		}


		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

		T& operator[](size_t pos)
		{
			assert(pos < size());

			return _start[pos];
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t old_size = size();
				T* tmp = new T[n];
				//memcpy(tmp, _start,size()*sizeof(T));
				for (size_t i = 0; i < size(); ++i)
				{
					tmp[i] = _start[i];
				}
				delete[] _start;

				_start = tmp;
				_finish = tmp + old_size;
				_end_of_storage = tmp + n;
			}
		}

		void resize(size_t n, const T& val = T())
		{
			if (n < size())
			{
				_finish = _start + n;
			}
			else
			{
				reserve(n);
				while (_finish < _start + n)
				{
					*_finish = val;
					++_finish;
				}
			}
		}

		const T& operator[](size_t pos) const
		{
			assert(pos < size());

			return _start[pos];
		}

		void push_back(const T& x)
		{
			if (_finish == _end_of_storage)
			{
				reserve(capacity() == 0 ? 1 : 2 * capacity());
			}

			*_finish = x;
			++_finish;
		}

		iterator insert(iterator pos, const T& x = T())
		{
			assert(pos <= _finish);

			size_t len = pos - _start;
			if (_finish == _end_of_storage)
			{
				reserve(capacity() == 0 ? 1 : 2 * capacity());
				pos = _start + len;//更新pos位置，避免因为扩容导致pos还指向旧空间
			}

			iterator end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				--end;
			}
			*pos = x;
			++_finish;

			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos < _finish);

			auto it = pos + 1;
			while (it != _finish)
			{
				*(it - 1) = *it;
				++it;
			}
			--_finish;
			return pos;
		}

	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;
	};

	void test_vector1()
	{
		xsc::vector<int> iv;
		iv.push_back(1);
		iv.push_back(2);
		iv.push_back(3);
		iv.push_back(4);
		iv.push_back(5);
		iv.push_back(6);
		iv.push_back(7);

		for (auto e : iv)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test_vector2()
	{
		xsc::vector<int> iv;
		iv.push_back(1);
		iv.push_back(2);
		iv.push_back(3);
		iv.push_back(4);

		for (auto e : iv)
		{
			cout << e << " ";
		}
		cout << endl;

		iv.insert(iv.begin() + 1, 40);

		for (auto e : iv)
		{
			cout << e << " ";
		}
		cout << endl;

		iv.insert(iv.begin(), 100);
		for (auto e : iv)
		{
			cout << e << " ";
		}
		cout << endl;
		iv.insert(iv.end(), 1200);
		for (auto e : iv)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test_vector3()
	{
		xsc::vector<int> iv;
		iv.push_back(1);
		iv.push_back(2);
		iv.push_back(3);
		iv.push_back(4);
		iv.push_back(5);

		for (auto e : iv)
		{
			cout << e << " ";
		}
		cout << endl;

		int x = 0;
		cin >> x;
		auto pos = find(iv.begin(), iv.end(), x);
		if (pos != iv.end())
		{
			iv.insert(pos, 40);
			/**(pos + 1) *= 10;*/
			*pos *= 10;
		
			
			for (auto e : iv)
			{
				cout << e << " ";
			}
			cout << endl;
		}

		cout << endl;
	}

	void test_vector4()
	{
		xsc::vector<int> iv;
		iv.push_back(1);
		iv.push_back(2);
		iv.push_back(3);
		iv.push_back(4);
		/*iv.push_back(4);
		iv.push_back(5);*/

		for (auto e : iv)
		{
			cout << e << " ";
		}
		cout << endl;

		auto p = iv.begin();
		while (p != iv.end())
		{
			if (*p % 2 == 0)
			{
				p = iv.erase(p);
			}
			else
			{
				++p;
			}
		}

		for (auto e : iv)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test_vector5()
	{
		int i = int();
		int j = int(1);
		int k(2);

		cout << i << endl;
		cout << j << endl;
		cout << k << endl;

	}

	void test_vector6()
	{
		xsc::vector<int> iv(5);
		xsc::vector<string> sv(5, "11111111111111");

		xsc::vector<int> iv2(10, 1);
	}

	void test_vector7()
	{
		xsc::vector<string> si(4, "1111111111111111111");
		for (auto& e : si)
		{
			cout << e << " ";
		}
		cout << endl;

		si.push_back("1111111111111111111");
		for (auto& e : si)
		{
			cout << e << " ";
		}
		cout << endl;
	}

}